package com.ccx.coachsite;

import java.util.Timer;
import java.util.TimerTask;

import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.ui.login.LoginActivity;
import com.ccx.coachsite.ui.softmgr.SoftMgrSearchView;
import com.ccx.coachsite.ui.softmgr.SoftMgrSoftListView;
import com.ccx.coachsite.util.SharedPreferencesUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class MainMenuActivity extends Activity implements OnClickListener {

	private boolean isExpert = false;
	private Button btnMe, btnApp, btnExpert, btnTutorial, btnClient, btnMore;
	private boolean isExit = false;
	private boolean hasTask = false;
	Timer tExit = new Timer();
	TimerTask exitTask = new TimerTask() {

		@Override
		public void run() {
			isExit = false;
			hasTask = true;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		isExpert = SharedPreferencesUtil.getBoolean(this,
				SharedKey.Shared_Is_Expert);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_activity_view);
		btnMe = (Button) findViewById(R.id.main_btn_me);
		btnApp = (Button) findViewById(R.id.main_btn_app);
		btnExpert = (Button) findViewById(R.id.main_btn_expert);
		if (isExpert)
			btnExpert.setBackgroundResource(R.drawable.main_expert_btn);
		btnTutorial = (Button) findViewById(R.id.main_btn_tutorial);
		btnMore = (Button) findViewById(R.id.main_btn_more);
		btnClient = (Button) findViewById(R.id.main_btn_client);

		if (!isExpert)
			btnClient.setVisibility(View.INVISIBLE);

		btnMe.setOnClickListener(this);
		btnApp.setOnClickListener(this);
		btnExpert.setOnClickListener(this);
		btnTutorial.setOnClickListener(this);
		btnMore.setOnClickListener(this);
		btnClient.setOnClickListener(this);
		
		//加载缓存数据
		SoftMgrSoftListView.initCacheData();
		SoftMgrSearchView.initCacheData();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 0 && resultCode == 1) {
			finish();

			Intent intent = new Intent(this, LoginActivity.class);
			startActivity(intent);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (isExit == false) {
				isExit = true;
				Toast.makeText(this, R.string.main_tip_back_exit,
						Toast.LENGTH_SHORT).show();
				if (!hasTask) {
					tExit.schedule(exitTask, 2000);
				}
				return true;
			} else {
				// TODO 检测相关需要保存的资源
				finish();
				System.exit(0);
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(this, CoachSiteMainActivity.class);
		if (v.equals(btnMe)) {
			intent.putExtra("SelectedPage", 5);
			intent.putExtra("args", 0);
		} else if (v.equals(btnMore)) {
			intent.putExtra("SelectedPage", 5);
			intent.putExtra("args", 1);
		} else if (v.equals(btnClient)) {
			intent.putExtra("SelectedPage", 5);
			intent.putExtra("args", 2);
		} else if (v.equals(btnApp))
			intent.putExtra("SelectedPage", 2);
		else if (v.equals(btnExpert))
			intent.putExtra("SelectedPage", 4);
		else if (v.equals(btnTutorial))
			intent.putExtra("SelectedPage", 3);
		startActivityForResult(intent, 0);
	}

}
